package com.asiainfo.dacp.datastash.domain.state.repos;

import com.asiainfo.dacp.datastash.domain.state.models.DataStashUnitStateLog;
import com.asiainfo.dacp.datastash.orm.dao.state.DataStashUnitStateLogDao;
import com.asiainfo.dacp.datastash.orm.po.state.DataStashUnitStateLogPo;
import com.asiainfo.dacp.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by winter on 2017/12/25.
 */
@Repository
public class DataStashUnitStateLogRepository {

    @Autowired
    private DataStashUnitStateLogDao unitStateLogDao;

    public Page<DataStashUnitStateLog> findUnitStateLog(int pageNum, int pageSize,
                                                        final String unitCode, final String batchNo, final String isTurnover,
                                                        final String state,final String sortColumn,final String desc) throws Exception {
        //TODO 增加按创建时间逆序排序
        Pageable pageable = new PageRequest(pageNum, pageSize);
        //TODO 获取对象应该是一个包含字段的丰满对象
        Page<DataStashUnitStateLogPo> tablePage = unitStateLogDao.findAll(new Specification<DataStashUnitStateLogPo>() {
            public Predicate toPredicate(Root<DataStashUnitStateLogPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //TODO 增加过滤条件
                List<Predicate> predicate = new ArrayList<Predicate>();
                Predicate[] p = new Predicate[predicate.size()];
                if (!"all".equals(unitCode)) {
                    predicate.add(cb.like(root.get("unitCode").as(String.class), "%" + unitCode + "%"));
                }
                if (!"all".equals(batchNo)) {
                    predicate.add(cb.like(root.get("batchNo").as(String.class), "%" + batchNo + "%"));
                }
                if (!"all".equals(isTurnover)) {
                    predicate.add(cb.equal(root.get("isTurnover").as(String.class), isTurnover));
                }
                if (!"all".equals(state)) {
                    predicate.add(cb.equal(root.get("state").as(String.class), state));
                }
                if ("desc".equals(desc)) {
                    query.orderBy(cb.desc(root.get(sortColumn).as(
                            String.class)));
                } else if ("asc".equals(desc)) {
                    query.orderBy(cb.asc(root.get(sortColumn).as(
                            String.class)));
                }
                query.where(cb.and(predicate.toArray(p)));
                return query.getRestriction();
            }
        }, pageable);
        List<DataStashUnitStateLog> list = new ArrayList<DataStashUnitStateLog>();
        Iterator<DataStashUnitStateLogPo> iter = tablePage.iterator();
        while (iter.hasNext()) {
            DataStashUnitStateLogPo po = iter.next();
            DataStashUnitStateLog vo = BeanConvertUtils.copyBean(po, DataStashUnitStateLog.class);
            list.add(vo);
        }
        Page<DataStashUnitStateLog> page = new PageImpl<DataStashUnitStateLog>(list, pageable, tablePage.getTotalElements());
        return page;
    }
}